<template>
  <div class="admin-container" :class="theme">
    <!-- 侧边导航栏 -->
    <aside class="admin-sidebar">
      <div class="sidebar-header">
        <h1>单词管理后台</h1>
        <div class="theme-switcher">
          <button @click="setTheme('light')" :class="{ active: theme === 'light' }" title="浅色模式">
            <svg viewBox="0 0 24 24" width="16" height="16">
              <path fill="currentColor" d="M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5S14.76,7,12,7L12,7z" />
            </svg>
          </button>
          <button @click="setTheme('dark')" :class="{ active: theme === 'dark' }" title="深色模式">
            <svg viewBox="0 0 24 24" width="16" height="16">
              <path fill="currentColor"
                d="M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36c-0.98,1.37-2.58,2.26-4.4,2.26 c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z" />
            </svg>
          </button>
        </div>
      </div>
      <nav class="sidebar-nav">
        <router-link v-for="item in navItems" :key="item.path" :to="item.path" active-class="active">
          <component :is="item.icon" />
          <span>{{ item.title }}</span>
        </router-link>
      </nav>
    </aside>

    <!-- 主内容区 -->
    <main class="admin-main">
      <header class="admin-header">
        <div class="header-left">
          <button class="menu-toggle" @click="toggleSidebar">
            <svg viewBox="0 0 24 24" width="20" height="20">
              <path fill="currentColor" d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" />
            </svg>
          </button>
          <h2>{{ currentRouteTitle }}</h2>
        </div>
        <div class="header-right">
          <div class="user-info">
            <img :src="user.avatar" alt="用户头像" class="user-avatar">
            <span class="user-name">{{ user.name }}</span>
          </div>
          <button class="logout-btn" @click="logout">
            <svg viewBox="0 0 24 24" width="18" height="18">
              <path fill="currentColor"
                d="M16,17V14H9V10H16V7L21,12L16,17M14,2A2,2 0 0,1 16,4V6H14V4H5V20H14V18H16V20A2,2 0 0,1 14,22H5A2,2 0 0,1 3,20V4A2,2 0 0,1 5,2H14Z" />
            </svg>
          </button>
        </div>
      </header>

      <div class="admin-content">
        <!-- 动态路由内容 -->
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, watch, inject, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  Home as HomeIcon,
  Book as BookIcon,
  Users as UsersIcon,
  Settings as SettingsIcon,
  FileText as FileTextIcon,
  BarChart2 as StatsIcon
} from 'lucide-vue-next'
import { getUserData } from '../../api/authService.js';
const userId = localStorage.getItem("userId");
const theme = inject("theme", ref('light'))
const setTheme = (newTheme) => {
  theme.value = newTheme
}
console.log(userId, 'userId');
const router = useRouter()
const route = useRoute()
const adminUserDetailData = ref({});

// 用户信息
const user = ref({
  name: adminUserDetailData.value.username,
  avatar: '../../../public/avatar/' + adminUserDetailData.value.avatar + '.png'
})

// 导航菜单项
const navItems = ref([
  { path: '/Home/Dashboard', title: '仪表盘', icon: HomeIcon },
  { path: '/Home/words', title: '单词管理', icon: BookIcon },
  // { path: '/admin/stats', title: '分类管理', icon: FileTextIcon },
  { path: '/Home/User', title: '用户管理', icon: UsersIcon },
  { path: '/Home/LogViewerStudy', title: '学习统计', icon: StatsIcon },
  // { path: '/admin/settings', title: '系统设置', icon: SettingsIcon },ArticleAdminPage
  { path: '/Home/LogViewer', title: '日志信息', icon: FileTextIcon },
  { path: '/Home/ArticleAdminPage', title: '文章管理', icon: FileTextIcon },
  { path: '/Home/Sentences', title: '例句管理', icon: FileTextIcon },
  { path: '/Home/SynonymsAntonyms', title: '同义词反义词管理', icon: FileTextIcon },
])

// 当前路由标题
const currentRouteTitle = computed(() => {
  const item = navItems.value.find(item => item.path === route.path)
  return item ? item.title : '单词管理后台'
})

// 侧边栏折叠状态
const isSidebarCollapsed = ref(false)

// 切换侧边栏
const toggleSidebar = () => {
  isSidebarCollapsed.value = !isSidebarCollapsed.value
}

// 登出
const logout = () => {
  // 这里添加登出逻辑
  localStorage.removeItem('token');
  router.push('/login')
}

// 监听路由变化
watch(() => route.path, () => {
  // 在小屏幕设备上，路由变化时自动折叠侧边栏
  if (window.innerWidth < 768) {
    isSidebarCollapsed.value = true
  }
})
const loadGetUserDetailData = async () => {
  try {
    const res = await getUserData(userId);
    // console.log(res, '获取用户数据');
    if (res) {
      adminUserDetailData.value = res;
      updateUserData()
      // console.log(adminUserDetailData.value, '获取用户数据');
    } else {
      ElMessage.error(res.message || "获取用户详细信息失败");
    }
  } catch (error) {
    ElMessage.error("获取失败: ");
  } finally {
    // loading.value = false;
  }
};
const updateUserData = async () => {
  user.value = {
    name: adminUserDetailData.value.username,
    avatar: '../../../public/avatar/' + adminUserDetailData.value.avatar + '.png'
  }
}
onMounted(() => {
  loadGetUserDetailData()
})
</script>

<style scoped>
.admin-container {
  --bg-color: #f5f7fa;
  --sidebar-bg: #ffffff;
  --header-bg: #ffffff;
  --content-bg: #ffffff;
  --text-color: #333333;
  --text-secondary: #666666;
  --primary-color: #4361ee;
  --primary-light: #e6e9ff;
  --border-color: #e1e4e8;
  --hover-bg: #f1f3f5;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --card-bg: #ffffff;
  --success-color: #28a745;
  --warning-color: #ffc107;
  --danger-color: #dc3545;
  --info-color: #17a2b8;

  display: flex;
  min-height: 100vh;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: all 0.3s ease;
}

.admin-container.dark {
  --bg-color: #1a1a1a;
  --sidebar-bg: #2d2d2d;
  --header-bg: #2d2d2d;
  --content-bg: #2d2d2d;
  --text-color: #e0e0e0;
  --text-secondary: #8e8e8e;
  --primary-color: #4895ef;
  --primary-light: #2a3a6e;
  --border-color: #383838;
  --hover-bg: #383838;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  --card-bg: #383838;
}

/* 侧边栏样式 */
.admin-sidebar {
  width: 240px;
  background-color: var(--sidebar-bg);
  box-shadow: var(--shadow);
  transition: all 0.3s ease;
  z-index: 100;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1.2rem;
  color: var(--primary-color);
}

.theme-switcher {
  display: flex;
  gap: 0.5rem;
}

.theme-switcher button {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.3rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}

.theme-switcher button.active {
  color: var(--primary-color);
  background-color: var(--hover-bg);
}

.theme-switcher button:hover {
  background-color: var(--hover-bg);
}

.sidebar-nav {
  flex: 1;
  padding: 1rem 0;
  overflow-y: auto;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  padding: 0.8rem 1.5rem;
  color: var(--text-color);
  text-decoration: none;
  transition: all 0.2s ease;
  gap: 0.8rem;
}

.sidebar-nav a:hover {
  background-color: var(--hover-bg);
}

.sidebar-nav a.active {
  background-color: var(--primary-light);
  color: var(--primary-color);
  border-right: 3px solid var(--primary-color);
}

.sidebar-nav svg {
  width: 1.2rem;
  height: 1.2rem;
}

/* 主内容区样式 */
.admin-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.admin-header {
  background-color: var(--header-bg);
  padding: 0 1.5rem;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  z-index: 90;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.menu-toggle {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-color);
  display: none;
}

.menu-toggle svg {
  width: 1.2rem;
  height: 1.2rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
}

.user-name {
  font-weight: 500;
}

.logout-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-color);
  padding: 0.3rem;
  border-radius: 4px;
}

.logout-btn:hover {
  background-color: var(--hover-bg);
}

.admin-content {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
  background-color: var(--content-bg);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .admin-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
  }

  .admin-sidebar.active {
    transform: translateX(0);
  }

  .menu-toggle {
    display: block;
  }
}

@media (max-width: 576px) {
  .admin-header {
    padding: 0 1rem;
  }

  .admin-content {
    padding: 1rem;
  }
}
</style>